vim.g.mapleader = ";"
vim.g.maplocalleader = ";"

local map = vim.api.nvim_set_keymap
local opt = {
  noremap = true,
  silent = true
}

local function press_quit()
  local wins = vim.api.nvim_tabpage_list_wins(0);
  local wins_cnt = #wins;

  local close_ft = { 'NvimTree', "toggleterm", "FTerm" }
  local filetype = vim.api.nvim_buf_get_option(0, "filetype")
  for _, value in ipairs(close_ft) do
    if filetype == value then
      goto force_quit
    end
  end

  if wins_cnt == 1 then
    goto nothing
  end

  ::force_quit::
  vim.api.nvim_command('q')
  goto finally

  ::nothing::
  vim.notify("Quit will kill the program. Please use :q to exit.")

  ::finally::
end

if vim.g.neovide ~= nil then
  if vim.fn.has('win32') == 1 then
    vim.api.nvim_command([[
     set guifont=CaskaydiaCove\ Nerd\ Font\ Mono:h11
    ]])
  else
    vim.api.nvim_command([[
     set guifont=Hack\ Nerd\ Font\ Mono:h11:#h-none
    ]])
  end
  vim.g.neovide_cursor_vfx_mode = "sonicboom"


  vim.api.nvim_create_user_command("NeovideQuit", function() press_quit() end, {})
  map("n", "q", "<CMD>NeovideQuit<CR>", opt)
else
  map("n", "q", ":q<CR>", opt)
end

map("n", "LS", "^", opt)
map("n", "LE", "$", opt)
map("v", "LS", "^", opt)
map("v", "LE", "$", opt)

map("n", "<leader>w", ":w<CR>", opt)
map("n", "<leader>q", ":qa<CR>", opt)

map("n", "x", '"_x', opt)

map("n", "<C-j>", "<C-w>j", opt)
map("n", "<C-k>", "<C-w>k", opt)
map("n", "<C-h>", "<C-w>h", opt)
map("n", "<C-l>", "<C-w>l", opt)

map('n', "<leader><", "<CMD>vertical resize +10<CR>", opt)
map('n', '<leader>>', '<CMD>vertical resize -10<CR>', opt)
map('n', '<leader>+', '<CMD>resize +10<CR>', opt);
map('n', '<leader>_', '<CMD>resize -10<CR>', opt);

-- 顶部状态栏
map("n", "<leader>[", ":BufferLineCyclePrev<CR>", opt)
map("n", "<leader>]", ":BufferLineCycleNext<CR>", opt)
map("n", "<leader><s-tab>", ":BufferLineCyclePrev<CR>", opt)
map("n", "<leader><tab>", ":BufferLineCycleNext<CR>", opt)
map("n", "<leader>1", ":lua require('bufferline').go_to_buffer(1, true)<CR>", opt)
map("n", "<leader>2", ":lua require('bufferline').go_to_buffer(2, true)<CR>", opt)
map("n", "<leader>3", ":lua require('bufferline').go_to_buffer(3, true)<CR>", opt)
map("n", "<leader>4", ":lua require('bufferline').go_to_buffer(4, true)<CR>", opt)
map("n", "<leader>5", ":lua require('bufferline').go_to_buffer(5, true)<CR>", opt)
map("n", "<leader>6", ":lua require('bufferline').go_to_buffer(6, true)<CR>", opt)
map("n", "<leader>7", ":lua require('bufferline').go_to_buffer(7, true)<CR>", opt)
map("n", "<leader>8", ":lua require('bufferline').go_to_buffer(8, true)<CR>", opt)
map("n", "<leader>9", ":lua require('bufferline').go_to_buffer(9, true)<CR>", opt)
map("n", "<leader>bc", ":bd<CR>", opt)
map("n", "<leader>bp", ":BufferLinePickClose<CR>", opt)
map("n", "<leader>co", ":BufferLineCloseOthers<CR>", opt)
map("n", "<leader>cl", ":BufferLineCloseLeft<CR>", opt)
map("n", "<leader>cr", ":BufferLineCloseRight<CR>", opt)

-- Format
map('n', '<leader>fm', '<CMD>lua vim.lsp.buf.format { async = true }<CR>', opt)

-- lazygit
map('n', '<leader>lg', '<CMD>lua require("FTerm").run("lazygit")<CR>', opt)

-- Symbols Outline
-- map('n', "<leader>v", '<CMD>SymbolsOutline<CR>', opt)

-- 查询
map('n', "<leader>ff", '<CMD>Telescope find_files<CR>', opt)
map('n', "<leader>fg", '<CMD>Telescope live_grep<CR>', opt)
map('n', "<leader>fb", '<CMD>Telescope buffers<CR>', opt)
map('n', "<leader>fh", '<CMD>Telescope help_tags<CR>', opt)
map('n', "<leader>fp", '<CMD>Telescope projects<CR>', opt)

